<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="reload">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="close">
                            <option value="">是否关闭</option>
                            <option value="1">未关闭</option>
                            <option value="2">已关闭</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="keys" placeholder="请输入课程标题" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-sm" id="more_{{d.id}}"><i class="layui-icon layui-icon-triangle-d"></i></button>
</script>
<script type="text/html" id="table-close">
    <input type="checkbox" name="close" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="table-close" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.close == 2 ? 'checked' : '' }}>
</script>

<script type="text/html" id="table-fabu">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="table-fabu" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="table-type">
    {{#if (d.type == '1') { }}
    <span>全员可见</span>
    {{# }else if(d.type == '2'){ }}
    <span>指定可见</span>
    {{# } }}
</script>
<script type="text/html" id="user-image">
    <div class="user-info-head">
        <img src="{{d.thumb}}" width="40px" height="80px" alt="">
    </div>
</script>
<script type="text/html" id="create-time">
    {{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery','common', 'laydate', 'dropdown'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let laydate = layui.laydate;
        let dropdown = layui.dropdown;

        laydate.render({
            elem: '#date'
            ,trigger: 'click'
            ,type: 'date'
        });

        laydate.render({
            elem: '#date1'
            ,trigger: 'click'
            ,type: 'date'
        });

        let cols = [
            [{
                    title: 'ID',
                    field: 'id',
                    align: 'center',
                    fixed: 'left',
                    width:'10%'
                },
                {
                    title: '排序',
                    field: 'sort',
                    align: 'center',
                    width:'10%',
                    fixed: 'left',
                    edit:'text'
                },
                {
                    title: '课程名称',
                    field: 'title',
                    align: 'center',
                    fixed: 'left',
                    width:'20%'
                },
                {
                    title: '是否关闭',
                    field: 'close',
                    align: 'center',
                    templet:'#table-close',
                    width:'10%'
                },
                {
                    title: '是否发布',
                    field: 'image',
                    align: 'center',
                    templet: '#table-fabu',
                    width:'10%'
                },
                {
                    title: '可见范围',
                    field: 'type',
                    align: 'center',
                    templet:'#table-type',
                    width:'10%'
                },
                {
                    title: '课程封面',
                    field: 'image',
                    align: 'center',
                    templet: '#user-image',
                    width:'10%'
                },
                {
                    title: '创建时间',
                    field: 'add_time',
                    align: 'center',
                    templet: '#create-time',
                    width:'20%'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width:'10%'
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Learning/load',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '导出课程学习明细' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            },{
                title: '导出课程明细' //标题
                ,layEvent: 'export1' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }],
            done: function(res, curr, count) {
                for (var i = 0; i < res.data.length; i++) {
                    dropdown.render({
                        elem: '#more_' + res.data[i].id,
                        data: [{
                            title: '修改',
                            id: res.data[i].id,
                            type: 1,
                        }, {
                            title: '学员',
                            id: res.data[i].id,
                            type: 2
                        }, {
                            title: '分享二维码',
                            id: res.data[i].id,
                            course_title: res.data[i].title,
                            type: 3
                        }, {
                            title: '培训安排',
                            id: res.data[i].id,
                            type: 4
                        }, {
                            title: '删除',
                            id: res.data[i].id,
                            type: 5
                        }, {
                            title: '导出',
                            id: res.data[i].id,
                            type: 6
                        }],
                        id: '#more_' + res.data[i].id,
                        click: function(obj) {
                            if (obj.type == 1) {

                                layer.open({
                                    type: 2,
                                    title: '修改',
                                    shade: 0.1,
                                    area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                                    content: '/Learning/edit?id='+obj.id
                                });

                            } else if (obj.type == 3) {
                                let loading = layer.load();
                                $.getJSON('/Learning/getQrCode?id='+obj.id, function(json){
                                    layer.close(loading);
                                    layer.open({
                                        type: 1,
                                        title: '分享二维码',
                                        shade: 0.1,
                                        area: [common.isModile()?'100%':'280px', '400px'],
                                        content: '<img src="'+json.data.url+'" alt="">',
                                        btn:['下载'],
                                        yes: function(index, layero){
                                            //按钮【按钮一】的回调
                                            window.open('/Learning/downloadQrCode?id='+obj.id+'&title='+obj.course_title, '_blank');
                                        }
                                    });
                                });

                            } else if(obj.type == 2) {
                                parent.layui.admin.addTab('learninguser'+obj.id, "E-learning学员", '/Learning/members?id='+obj.id)
                            } else if(obj.type == 4) {
                                parent.layui.admin.addTab('learningunit'+obj.id, "E-learning培训安排", '/Learning/step?learning_id='+obj.id)
                            } else if(obj.type == 5) {
                                layer.confirm('确定要删除课程吗?', {
                                    icon: 3,
                                    title: '提示'
                                }, function(index) {
                                    layer.close(index);
                                    let loading = layer.load();
                                    $.ajax({
                                        url: "/Learning/delete",
                                        data:{id:obj.id},
                                        dataType: 'json',
                                        type: 'post',
                                        success: function(result) {
                                            layer.close(loading);
                                            if (result.status == 0) {
                                                layer.msg(result.msg, {
                                                    icon: 1,
                                                    time: 1000
                                                }, function() {
                                                    table.reload('user-table');
                                                });
                                            } else {
                                                layer.msg(result.msg, {
                                                    icon: 2,
                                                    time: 1000
                                                });
                                            }
                                        }
                                    })
                                });
                            } else if(obj.type == 6) {
                                layer.confirm('确定要导出吗?', {
                                    icon: 3,
                                    title: '提示'
                                }, function(index) {
                                    layer.close(index);
                                    window.open("{:url('export')}?id="+obj.id, "_blank");
                                });
                            }
                        }
                    });
                }
            }
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });

        //监听单元格编辑
        table.on('edit(user-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
            $.post("{:url('sort')}", {id: data.id,sort:value}, function (result) {
                if(result.status == 0) {
                    table.reload("user-table");
                } else {
                    layer.msg(result.msg,{icon:2,time:1000});
                    table.reload("user-table");
                }
            });
        });

        form.on('switch(table-close)', function(obj) {
            $.post("{:url('close')}", {id: this.value}, function (result) {
                if(result.status != 0) {
                    layer.tips(result.msg, obj.othis);
                }

            });
        });


        form.on('switch(table-fabu)', function(obj) {
            $.post("{:url('fabu')}", {id: this.value}, function (result) {
                if(result.status != 0) {
                    layer.tips(result.msg, obj.othis);
                }

            });
        });


        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'export') {
                window.export();
            } else if (obj.event === 'export1') {
                window.export1();
            }
        });

        window.add = function() {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Learning/add'
            });
        }


        // 数据导出
        window.export = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出课程学习明细吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Learning/exportAll",
                    data:{keys:field.keys, close:field.close},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.status == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 2000
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }
                })
            });
        };

        window.export1 = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出课程明细吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('exportLearning')}?keys="+field.keys+'&close='+field.close, "_blank");
            });
        };

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
